using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace NGSaaS
{

    public partial class Index: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            setServerName();
            tb_password.TextMode = TextBoxMode.Password; //added by Jeya Prabha on 27-Aug-2007 
            tb_server.Focus();
        }
        private void setServerName()
        {
            StreamReader sr = null;
            try
            {
                string fName = HttpContext.Current.Server.MapPath(".") + "\\maxblox.ini";
                if (File.Exists(fName))
                {
                    bool powerOn = false; string temp = "";
                    sr = new StreamReader(fName);
                    do
                    {
                        temp = sr.ReadLine();
                        if (temp.ToLower().Trim() == "[server]") powerOn = true;
                        if (temp.ToLower().IndexOf("name=") > -1 && powerOn)
                        {
                            tb_server.Text = temp.Substring(temp.ToLower().IndexOf("name=") + 5).Trim();
                            break;
                        }
                    }
                    while (sr.EndOfStream == false);
                    sr.Close(); sr = null;
                }
            }
            catch (Exception e)
            {
                if (sr != null) sr.Close();
                sr = null;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (tb_user_id.Text == string.Empty || tb_server.Text == string.Empty)
            {
                lbl_msg.Text = "Please enter Server Name and User id";
            }
            else
            {
                //string dbcon = "";
                security objSec = new security();
                string dbcon = "server=" + tb_server.Text + ";database=" + objSec.getServerCatalog() + ";uid=" + tb_user_id.Text + ";password=" + tb_password.Text;
                //string dbcon = @"Data Source=.;Initial Catalog=NGMaster;Trusted_Connection=Yes;";
                HDDatabase hddb = new HDDatabase(dbcon);
                if (hddb.isValidConnection())
                {
                    HttpContext.Current.Session["dbcon"] = dbcon;
                    HttpContext.Current.Session["servercon"] = dbcon;
                    HttpContext.Current.Session["userid"] = "admin";
                    Session["user_name"] = "Administrator MaxBlox";
                    Session["Welcome_Title"] = "Administrator MaxBlox"; // Added by 'Imran Gani' on 03-Jan-2013, for dynamic welcome title.
                    Session["user_role"] = "Database Administrator";
                    HttpContext.Current.Session["Theme"] = GlobalValues.DefaultTheme;
                    Response.Redirect("Welcome.aspx?id=default");
                }
                else
                {
                    lbl_msg.Text = "Server does not exists or User Name/Password mismatch!";
                }
            }
        }
        protected void btn_reset1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }
}
}
